<?php
namespace Mobile\Controller;
use Think\Controller;
use Common\Model\Movie;

class SeatController extends Controller {
    /*座位页面*/
    public function index(){
        //var_dump($_GET);
        $user = $_GET['uid'];
        $movie = $_GET['mid'];
        $screening = $_GET['sid'];
        $price = $_GET['spr'];

        //场次信息
        $screeningInfo = M('screening')->where(['id'=>$screening])->find();
        //根据场次，查询已售出座位
        $seatInfo=M('order_item')->query("select seat_row,seat_column from pre_order_item  where screening_id =".$screening);
        //分割排和做为单独的数组
        foreach($seatInfo as $val){
            $list['r']= explode(',',$val['seat_row']);
            $list['c']= explode(',',$val['seat_column']);
            $seatList[]=$list;
        }
        //var_dump($seatList);
        $seatSell=json_encode($seatList);
        //echo $seatSell;exit;
        //电影信息
        $list=M('movie')->where(['id'=>$movie])->select();
        foreach($list as $k=>$v)
        {
            $list[$k]['language']=Movie::langAlias($v['language']);
        }
        //var_dump($list);exit;
        $this->assign('user',$user);
        $this->assign('movie',$movie);
        $this->assign('screening',$screeningInfo);
        $this->assign('price',$price);
        $this->assign('list',$list);
        $this->assign('screening',$screening);
        $this->assign('seatSell',$seatSell);
        $this->display();
    }

    /*数据传递处理*/
    public function add(){
        session('member',1);
        $list = [];
        foreach($_POST['seat'] as $k=> $v){
            $list[$k]['row']= explode(',',$v)[0];
            $list[$k]['col']= explode(',',$v)[1];
        }

        //处理座位号，以便写入数据库
        $row = '';
        $col = '';
        foreach($list as $key => $value){
            $row .= $value['row'].',';
            $col .= $value['col'].',';
        }
        //数据库里写入数据
        $data['user_id'] = I('post.user');
        $data['member_id'] = session('member');
        $data['movie_id'] = I('post.movie');
        $data['screening_id'] = I('post.screening');
        $data['price'] = I('post.price');
        $data['quantity'] = count($list);
        $data['total_fee'] =  $data['price'] * $data['quantity'];
        $data['created_at'] = time();

        //订单写入数据库
        $order = M('Order');
        $info = $order->add($data);
        if($info){
            $date['order_id'] = $info;
            $date['screening_id'] = $data['screening_id'];
            $date['seat_row'] = trim($row,',');
            $date['seat_column'] = trim($col,',');
            $date['created_at'] = time();
            $info1 = M('OrderItem')->add($date);
            $result=[];
            if($info1){
                $result['status'] = true;
                $result['orderId'] = $info;
            }else{
                $result['status'] = false;
                $result['mess'] = 'no';
                M('Order')->where("id=".$info)->delete();
            }
            echo json_encode($result);
        }
    }
}

